/* eslint-disable */
let Toast = {}
Toast.install = function (Vue, options) {
  let opt = {
    defaultType: 'bottom',
    duration: '1500'
  }

  for (let property in options) {
    opt[property] = options[property]
  }

  Vue.prototype.$toast = function (tips, type, fn) {
    let curType = type ? type : opt.defaultType
    if (document.querySelector('.lx-toast')) {
        // 如果toast还在，则不再执行
        return
    }
    let toastTpl = Vue.extend({
      template: '<div class="toast-wrapper toast-' + curType + '"><span class="lx-toast">' + tips + '</span></div>'
    })
    let tpl = new toastTpl().$mount().$el
    document.body.appendChild(tpl)
    setTimeout(() => {
      document.body.removeChild(tpl)
      fn && fn()
    }, opt.duration)
  }

  let positions = ['bottom', 'center', 'top']
  positions.forEach(function (type) {
    Vue.prototype.$toast[type] = function (tips, fn) {
      return Vue.prototype.$toast(tips, type, fn)
    }
  })

  Vue.prototype.$loading = function (tips, type) {
    let load = document.querySelector('.lx-load-mark')

    if (type === 'close') {
      load && document.body.removeChild(load)
    } else {
      if (load) {
        // 如果loading还在，则不再执行
        return
      }
      /*let loadTpl = Vue.extend({
        template: '<div class="lx-load-mark"><div class="lx-load-box"><div class="lx-loading"><div class="loading_leaf loading_leaf_0"></div><div class="loading_leaf loading_leaf_1"></div><div class="loading_leaf loading_leaf_2"></div><div class="loading_leaf loading_leaf_3"></div><div class="loading_leaf loading_leaf_4"></div><div class="loading_leaf loading_leaf_5"></div><div class="loading_leaf loading_leaf_6"></div><div class="loading_leaf loading_leaf_7"></div><div class="loading_leaf loading_leaf_8"></div><div class="loading_leaf loading_leaf_9"></div><div class="loading_leaf loading_leaf_10"></div><div class="loading_leaf loading_leaf_11"></div></div><div class="lx-load-content">' + tips + '</div></div></div>'
      })*/
      let loadTpl = Vue.extend({
        template: `
          <div class="lx-load-mark">
            <div class="loading-box">
              <div class="toast-loading-img"><img src="" /></div>
              <div class="toast-loading-text">${tips}</div>
            </div>
          </div>
        `
      })
      let tpl = new loadTpl().$mount().$el
      document.body.appendChild(tpl)
    }
  }

  let flag = ['open', 'close']
  flag.forEach(function (type) {
    Vue.prototype.$loading[type] = function (tips) {
      return Vue.prototype.$loading(tips, type)
    }
  })
}
module.exports = Toast
